package Test;

/**
 * 递归实现
 * 阶乘
 */
public class Demo04_TestRecursion {
    public static void main(String[] args) {
        //计算阶乘
        System.out.println(factorial(5));
        System.out.println(fact(5));
    }

    //循环实现
    private static int factorial(int n) {
        int result = 1 ;
        for (int j = 1; j <= n ; j++) {
            result *= j;
        }
        return result;
    }

    //递归实现
    private static int fact(int i ){
        if (i == 0 ) return 1 ;
        return fact(i - 1 ) * i ;
    }
}
